<style>

</style>
<template>
  <el-dialog
    title="修改脚本"
    :visible.sync="dialogVisible"
    :close-on-press-escape="true"
    :close-on-click-modal="true"
    width="80%"
    :before-close="clearForm">

    <el-form
      label-width="130px"
      :model="saveData"
      ref="ruleForm"
    >
      <el-form-item label="项目CODE" prop="clazzName">
        <el-input :disabled="true" v-model="saveData.appCode"></el-input>
      </el-form-item>
      <el-form-item label="被Mock类" prop="clazzName">
        <el-input :disabled="true" v-model="saveData.clazzName"></el-input>
      </el-form-item>
      <el-form-item label="被Mock方法" prop="methodName">
        <el-input :disabled="true" v-model="saveData.methodName"></el-input>
      </el-form-item>
      <el-form-item label="被Mock方法参数" prop="descriptor">
        <el-input :disabled="true" v-model="saveData.descriptor"></el-input>
      </el-form-item>
      <el-form-item label="执行脚本" prop="script">
        <el-input rows="30" type="textarea" v-model="saveData.script"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
    <el-button @click="clearForm">关 闭</el-button>
    <el-button @click="update">更 新</el-button>
  </span>
  </el-dialog>
</template>
<script>
import {classScriptSave} from "@/api/classScript";

export default {
  name: "classScriptAddForm",
  filters: {},
  components: {},
  data() {
    return {
      dialogVisible: false,
      saveData: {
        tempQuery: '',
        appCode: '',
        clazzName: '',
        methodName: '',
        descriptor: '',
        script: '',
      }
    }
  },
  async created() {
  },
  mounted() {
  },
  methods: {
    load(data) {
      let vm = this
      if (data) {
        vm.saveData = vm.$cloneObj(data)
      }
      vm.dialogVisible = true
    },
    clearForm() {
      let vm = this
      try {
        vm.saveData = {
          tempQuery: '',
          appCode: '',
          clazzName: '',
          methodName: '',
          descriptor: '',
          script: '',
        }
        vm.$refs['ruleForm'].resetFields()
      } catch (e) {
        console.log(e)
      }
      vm.dialogVisible = false
    },
    update() {
      let vm = this
      this.$refs['ruleForm'].validate(async (valid) => {
        if (valid) {
          await vm.doSave(this.saveData)
        }
      })
    },
    async doSave(saveData) {
      let vm = this
      let ret = await classScriptSave(saveData);
      if (ret.ok) {
        vm.$message({type: 'success', message: '保存成功'});
      } else {
        vm.$message({type: 'error', message: '保存失败：' + ret.message});
      }
      vm.clearForm()
      vm.$parent.query()
    }
  }
}
</script>
